package OSMS.action.Boss;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import OSMS.dao.GoodsDao;
import OSMS.entity.Boss;
import OSMS.entity.Goods;

public class GoodsInsertServlet extends HttpServlet {
	
	private GoodsDao dao = new GoodsDao();
	
	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try {
			
			//从session中取得商家的bid
			request.setCharacterEncoding("utf-8");
			response.setCharacterEncoding("utf-8");
			HttpSession session = request.getSession();
			Object obj_user = session.getAttribute("boss");
			Boss boss = (Boss) obj_user;
			int bid = boss.getBid();
			
			//测试用
			//System.out.println("GoodsInsert获取到的"+bid);
			
			//获取insertgoods.jsp中文本框输入的数据
			String gname = request.getParameter("gname");
			String gprice_str = request.getParameter("gprice");
			float gprice = Float.parseFloat(gprice_str);
			String gtype = request.getParameter("gtype");
			String picture = request.getParameter("picture");
			String gdetail = request.getParameter("gdetail");
			int gstore = Integer.parseInt(request.getParameter("gstore"));
			
			Goods goods = new Goods();
			goods.setBid(bid);
			goods.setGname(gname);
			goods.setGprice(gprice);
			goods.setGtype(gtype);
			goods.setPicture(picture);
			goods.setGdetail(gdetail);
			goods.setGstore(gstore);
			
			
			boss.setBid(bid);
			
			// 2. 调用StudentDao-dao中的新增方法
			boolean flag = false;
			// 3. 跳转到list页面
			try {
				// 新增成功，跳转到list页面
				 flag = dao.addGoods(goods);
				 System.out.println(boss.getBid());
				 System.out.println(flag);
//				 request.getRequestDispatcher("BGList").forward(request, response);
			} catch(Exception e) {
				e.printStackTrace();
			}
		} catch(Exception ex) {
			ex.printStackTrace();
		}
		
		request.setCharacterEncoding("utf-8");	
		request.getRequestDispatcher("../boss/bossindex").forward(request, response);
	}

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.doGet(request, response);
	}
}